package game;

import java.awt.Point;
import java.util.List;
import java.util.ArrayList;

/**
 * Implements the Queen piece for chess and the Queen's problem
 * 
 * @author Nicole Waldrum
 * @date January 30, 2010
 * @version 1.0
 *
 * @author Mark Kelly
 * @date February 19, 2010
 * @version 2.0
 * 
 * @author Trevor Bastian
 * @date February 20, 2010
 * @version 2.0
 */
public class Queen extends Piece {	
	/**
	 * Creates a new instance of the queen
	 * 
	 * @param name the name of this Queen
	 * @param position the initial location of this Queen
	 * @param team the team that this Queen is on
	 */
	public Queen(String name, Point position, Team team) {
		super(name, position, team);
	}
	
	/**
	 * Creates a new instance of the queen
	 * 
	 * @param position the initial location of the Queen
	 * @param team the team that this Queen is on
	 */
	public Queen(Point position, Team team) {
		this((team == Team.WHITE) ? "WQ" : "BQ", position, team);
	}

	/**
	 * Assumes that 'isValidMove()' has been called and returns true first
	 * {@inheritDoc}
	 */
	@Override
	public List<Point> checkMovePattern(Point dest) {
		List<Point> ret = new ArrayList<Point>();
        Point p = this.getLocation();
        int dx = (int) ( dest . getX () - p . getX ());
        int dy = (int) ( dest . getY () - p . getY ());
        int i = ( Math . abs ( dx ) > Math . abs ( dy ) ) ? Math . abs ( dx ) : Math . abs ( dy );
        
        if ( dx != 0 )
            dx /= Math . abs ( dx );    // give dx a magnitude of 1
        if ( dy != 0 )
            dy /= Math . abs ( dy );    // give dy a magnitude of 1
        
        for ( int j = 0; j < i - 1; j++ ) {
            p = new Point ((((int) p . getX ()) + dx ), (((int) p . getY ()) + dy ));
            ret . add( p );
        }
        
        return ret;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean isValidMove(Piece piece, Point dest) {
		Point start = piece.getLocation();
		// rook isValidMove criteria
		if ( (int)( start . getX () - dest . getX ()) == 0 || (int)( start . getY () - dest . getY ()) == 0 )
			return super.isValidMove(piece, dest);

		// bishop isValidMove criteria
		else if ( Math . abs ( start . getX () - dest . getX ()) == Math . abs ( start . getY () - dest . getY ()))
			return super.isValidMove(piece, dest);

		return false;
	}
}
